package com.etone.eos.jeos;

import io.jafka.jeos.core.response.chain.TableRow;
import io.jafka.jeos.impl.EosApiRestClientImpl;
import io.jafka.jeos.impl.EosApiServiceGenerator;
import io.jafka.jeos.impl.EosChainApiService;

import java.util.LinkedHashMap;
import java.util.Map;

public class SmartEosApiRestClientImpl extends EosApiRestClientImpl implements SmartEosApi {

    private EosChainApiService smartApiService;

    public SmartEosApiRestClientImpl(String baseUrl) {
        super(baseUrl);
        smartApiService = EosApiServiceGenerator.createService(EosChainApiService.class, baseUrl);
    }

    public SmartEosApiRestClientImpl(String walletBaseUrl, String chainBaseUrl, String historyBaseUrl) {
        super(walletBaseUrl, chainBaseUrl, historyBaseUrl);
    }

    @Override
    public TableRow getSmartTableRows(String scope, String code, String table, Map<String, String> params) {
        LinkedHashMap<String, String> requestParameters = new LinkedHashMap<>(7);
        requestParameters.put("scope", scope);
        requestParameters.put("code", code);
        requestParameters.put("table", table);
        requestParameters.put("json", "true");
        if (params != null && params.size() > 0) {
            //添加额外的参数
            params.forEach((k, v) -> {
                requestParameters.put(k, v);
            });
        }
        return EosApiServiceGenerator.executeSync(smartApiService.getTableRows(requestParameters));
    }
}
